<template>
  <div class="item" style="width:100%;">
    <div class="title">{{ itemData.name }}</div>
    <div class="name">{{ itemData.manager }}</div>
    <el-dropdown @command="commandEvent">
      <span
        >操作
        <i class="el-icon-arrow-down"></i>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item command="edit" v-if="!itemData.isTop"
            >查看部门</el-dropdown-item
          >
          <el-dropdown-item command="del" v-if="!itemData.isTop"
            >删除部门</el-dropdown-item
          >
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import { companyDepartmentDelete } from '@/api/departments'
export default {
  props: {
    itemData: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    commandEvent (command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('Addshow', true, this.itemData, 'add')
          break
        // 删除事件处理
        case 'del':
          this.$confirm('您确定删除该部门吗？', '提示')
            .then(async () => {
              await companyDepartmentDelete(this.itemData.id)
              this.$message.success('删除成功')
              // 刷新父级列表数据
              this.$emit('getData')
            })
            .catch()
          break
        // 编辑事件处理
        case 'edit':
          // console.log('点击项数据itemData:', this.itemData)
          this.$bus.$emit('Addshow', true, this.itemData, 'edit')
          break
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.item {
  display: flex;
  .title {
    flex: 1;
  }
  .name {
    width: 100px;
  }
}
</style>
